package puzzle.masyu2.solver;

import java.util.List;

import puzzle.common.data.HatanException;
import puzzle.common.point.Direction;
import puzzle.common.point.Pnt;
import puzzle.common.point.Vct;
import puzzle.common.solver.Track;
import puzzle.common.solver.TrackTopology;
import puzzle.masyu2.data.MasyuBoard;

class AviodLoopTopologicalRule implements MasyuTopologicalRule {

	public boolean apply(MasyuBoard b, TrackTopology topology) throws HatanException {
		List<Track> tracks = topology.getTracks();

		if (tracks.size() == 1) {
			return false;
		}

		boolean isChanged = false;
		for (Track track : tracks) {
			switch (track.getEndDistance()) {
			case 1:
				isChanged |= process1(b, track);
				break;
			case 2:
				isChanged |= process2(b, track);
				break;
			}
		}
		return isChanged;
	}

	private boolean process1(MasyuBoard b, Track track) throws HatanException {
		Pnt p = track.getEnds().car;
		Vct v = p.vctTo(track.getEnds().cdr);
		Direction d = Direction.quad(v);
		return b.setForbiddenFlag(p.x, p.y, d);
	}

	private boolean process2(MasyuBoard b, Track track) throws HatanException {
		return false;
	}

}
